#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_freertos_hooks.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "esp_log.h"

#include "lvgl.h"

#include "lvgl_helpers.h"
//#include "demos/lv_demos.h"

#include "screen.h"


static const char* TAG = "screen";


static lv_obj_t* scr_qr_code;
static lv_obj_t* scr_main;

static lv_obj_t * meter;
static lv_meter_indicator_t * indic_min;
static lv_meter_indicator_t * indic_hour;

xQueueHandle screen_event_queue;


void screen_event_task(void *arg)
{
    screen_event_t screen_event;

    // wait main gui load finished
    vTaskDelay(2000 / portTICK_RATE_MS);  

    while (1)
    {
        xQueueReceive(screen_event_queue, &screen_event, portMAX_DELAY);
        switch(screen_event.type) {
            case S_EVENT_WIFI_NO_CONFIG:
                ESP_LOGI(TAG, "event: wifi no config");
                //lv_scr_load(scr_qr_code);
                break;
            case S_EVENT_WIFI_CONNECTED:
                ESP_LOGI(TAG, "event: wifi connected");
                break;
            case S_EVENT_UPDATE_CLOCK:
                ESP_LOGI(TAG, "event: update clock");
                set_clock_time();
                break;
        }

    }
    vTaskDelete(NULL);

}

static void set_clock_time()
{
    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);

    int minute = timeinfo.tm_min;
    int hour = timeinfo.tm_hour;
   
    int hour_offset;
    if (hour >= 12) {
        hour_offset = ((hour - 12) * 1.0 + minute / 60.0) * 5;
    } else{
        hour_offset = (hour * 1.0 + minute / 60.0) * 5;
    }
    
    lv_meter_set_indicator_value(meter, indic_hour, hour_offset);
    lv_meter_set_indicator_value(meter, indic_min, minute);
}

static void set_value(void * indic, int32_t v)
{
    lv_meter_set_indicator_value(meter, indic, v);
}

void disp_clock(lv_obj_t* parent)
{
    meter = lv_meter_create(parent);
    lv_obj_set_size(meter, 240, 240);
    lv_obj_center(meter);

    /*Create a scale for the minutes*/
    /*61 ticks in a 360 degrees range (the last and the first line overlaps)*/
    lv_meter_scale_t * scale_min = lv_meter_add_scale(meter);
    lv_meter_set_scale_ticks(meter, scale_min, 61, 1, 10, lv_palette_main(LV_PALETTE_GREY));
    lv_meter_set_scale_range(meter, scale_min, 0, 60, 360, 270);

    /*Create another scale for the hours. It's only visual and contains only major ticks*/
    lv_meter_scale_t * scale_hour = lv_meter_add_scale(meter);
    lv_meter_set_scale_ticks(meter, scale_hour, 12, 0, 0, lv_palette_main(LV_PALETTE_GREY));               /*12 ticks*/
    lv_meter_set_scale_major_ticks(meter, scale_hour, 1, 2, 20, lv_color_black(), 10);    /*Every tick is major*/
    lv_meter_set_scale_range(meter, scale_hour, 1, 12, 330, 300);       /*[1..12] values in an almost full circle*/

    LV_IMG_DECLARE(img_hand)

    /*Add a the hands from images*/
    indic_min = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);
    indic_hour = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);

    /*Create an animation to set the value*/
    // lv_anim_t a;
    // lv_anim_init(&a);
    // lv_anim_set_exec_cb(&a, set_value);
    // lv_anim_set_values(&a, 0, 60);
    // lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    // lv_anim_set_time(&a, 2000);     /*2 sec for 1 turn of the minute hand (1 hour)*/
    // lv_anim_set_var(&a, indic_min);
    // lv_anim_start(&a);

    // lv_anim_set_var(&a, indic_hour);
    // lv_anim_set_time(&a, 24000);    /*24 sec for 1 turn of the hour hand*/
    // lv_anim_set_values(&a, 0, 60);
    // lv_anim_start(&a);
    
    //lv_meter_set_indicator_value(meter, indic_hour, 10);
}

void main_screen()
{
    ESP_LOGI(TAG, "clock main screen start");

    lv_obj_t *scr_main = lv_disp_get_scr_act(NULL); 
    disp_clock(scr_main);
    
    // 创建二维码屏幕但未加载到显示
    LV_IMG_DECLARE(img_smart_config);
    scr_qr_code = lv_obj_create(NULL);  // 创建二维码屏幕但未加载到显示
    lv_obj_t * img1 = lv_img_create(scr_qr_code);
    lv_img_set_src(img1, &img_smart_config);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img1, 240, 240);



    lv_scr_load(scr_main);
    
}
